<?php 

/*
 * Following code will create a new pemesanan row
 * All pemesanan details are read from HTTP Post Request
 */

/* array for JSON response */
$response = array();

/* check for required fields */

if (isset($_POST['nama_pemesan']) && isset($_POST['alamat']) && isset($_POST['provinsi']) && isset($_POST['kota']) && isset($_POST['email']) && isset($_POST['telp']) ) {
    $nama_pemesan = $_POST['nama_pemesan'];
    $alamat = $_POST['alamat'];
    $provinsi = $_POST['provinsi'];
    $kota = $_POST['kota'];
    $email = $_POST['email'];
    $telp = $_POST['telp'];
    $id_ekspedisi = $_POST['id_ekspedisi'];

	/*
	$nama_pemesan = "cek1";
    $alamat = "cek2";
    $provinsi = "cek3";
    $kota = "cek4";
    $email = "popcroach@gmail.com";
    $telp = 23504358;
	*/
    require_once __DIR__ . '/db_connect.php';

    $db = new DB_CONNECT();

	$id_ekspedisi = 0;
	$qEks = mysql_query("SELECT e.* FROM ekspedisi e WHERE e.propinsi='".$provinsi."' AND e.kota='".$kota."'");
	while ($ro=mysql_fetch_array($qEks)){
	$id_ekspedisi = $ro[id_ekspedisi];     
	}
	
	
    $result = mysql_query("INSERT INTO pemesanan(tanggal, nama_pemesan, alamat, provinsi, kota, email, telepon, status, id_ekspedisi) VALUES (CURRENT_DATE, '$nama_pemesan', '$alamat', '$provinsi', '$kota', '$email', '$telp', 'Baru', $id_ekspedisi)");

    /* check if row inserted or not */
    if ($result) {
        /* successfully inserted into database */
        $response["success"] = 1;
        $response["message"] = "Pemesanan successfully created.";

        /* echoing JSON response */
        echo json_encode($response);
/*
$message  = 
"ID Pemesanan Anda : 2342342
Nama Pemesan : ".$nama_pemesan."
Alamat : ".$alamat."
Kota : ".$kota."
Provinsi : ".$provinsi."
Telepon : ".$telp."

Produk yg dipesain 
Produk yg dipesain 
";	
		$subject = "Detail Pemesanan Hanjuang";
		
		mail($email, $subject,
		$message, "From:" . "fathur_jtk09@yahoo.com");
*/
		/* echo "<hr>Thank you for using our mail form yo!"; */
		
		
    } else {
        /* failed to insert row */
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";
        
        /* echoing JSON response */
        echo json_encode($response);
    }
} else {    
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
	
    echo json_encode($response);
}
?>